package com.watayouxiang.qrcode.feature.qrcode_my;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.StringUtils;
import com.watayouxiang.androidutils.listener.OnSingleClickListener;
import com.watayouxiang.androidutils.page.BindingActivity;
import com.watayouxiang.androidutils.utils.ClickUtils;
import com.watayouxiang.androidutils.widget.TioToast;
import com.watayouxiang.httpclient.model.response.UserCurrResp;
import com.watayouxiang.httpclient.preferences.HttpCache;
import com.watayouxiang.qrcode.R;
import com.watayouxiang.qrcode.databinding.ActivityQrcodeMyBinding;
import com.watayouxiang.qrcode.feature.qrcode_decoder.QRCodeDecoderActivity;
import com.watayouxiang.qrcode.feature.qrcode_my.mvp.Contract;
import com.watayouxiang.qrcode.feature.qrcode_my.mvp.Presenter;

import java.io.File;

/**
 * <pre>
 *     author : TaoWang
 *     e-mail : watayouxiang@qq.com
 *     time   : 2020/12/09
 *     desc   : 我的二维码
 * </pre>
 */
public class MyQRCodeActivity extends BindingActivity<ActivityQrcodeMyBinding> implements Contract.View {

    private Presenter presenter;

    public static void start(Context context) {
        Presenter.checkPermission(() -> {
            Intent starter = new Intent(context, MyQRCodeActivity.class);
            context.startActivity(starter);
        });
    }

    @Override
    protected View statusBar_holder() {
        return binding.statusBar;
    }

    @Override
    protected Integer statusBar_color() {
        return Color.TRANSPARENT;
    }

    @Override
    protected Integer background_color() {
        return Color.parseColor("#498FF6");
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_qrcode_my;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = new Presenter(this);
        presenter.init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }

    @Override
    public void resetUI() {
        binding.titleBar.getIvRight().setOnClickListener(view -> {
            if (ClickUtils.isViewSingleClick(view)) {
                QRCodeDecoderActivity.start(MyQRCodeActivity.this);
            }
        });
        binding.ivAvatar.load_tioAvatar(null);
        binding.tvNick.setText("");
        binding.tvTip.setText("使用谭聊APP扫一扫，加我为好友");
        binding.tvDownload.setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View view) {
                File png = presenter.saveQRCode2Album(binding.clQrcode);
                if (png != null) {
                    TioToast.showShort("已成功保存至相册");
                } else {
                    TioToast.showShort("下载失败");
                }
            }
        });
    }

    @Override
    public void onUserCurrResp(UserCurrResp resp) {
        binding.ivAvatar.load_tioAvatar(HttpCache.getResUrl(resp.avatar));
        binding.tvNick.setText(StringUtils.null2Length0(resp.nick));
    }

    @Override
    public void onMyQRCodeGet(Bitmap result) {
        binding.ivMyQRCode.setImageBitmap(result);
    }
}
